﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtAspNet;

namespace HY.WEB.PQ
{
    public partial class mem_edit2 : PageBase
    {
        public string whe = "", path = "", tab = "pq_member";
        public string us, um, ubh;

        protected void Page_Load(object sender, EventArgs e)
        {
            us = Session["ID"].ToString();
            um = Session["user"].ToString();
            ubh = Session["bh"].ToString();

            if (!IsPostBack)
            {
                if (!String.IsNullOrEmpty(Request["sid"]))
                {
                     hidsid.Text = Request["sid"].Trim();                
                }
                fill();
                fill2();
                fill3();
                Regcomp();
            }
            else
            {
                fillcomp();
            }


        }
        void Regcomp()
        {


            ExtAspNet.PageContext.RegisterStartupScript
                (
                "var d=Ext.getCmp('" + ddlcomp.ClientID + "');" +
                " var oldInput=\"\";" +
                " d.listWidth=500; " +
                " d.addListener(\"specialkey\",function(f,e){ " +  //keyup
                " var newInput=document.getElementById(\"" + ddlcomp.ClientID + "\").value;" +
                 " if (oldInput==newInput) {return;} " +
                " oldInput=newInput;" +
                 " __doPostBack('loadcp',newInput);" +
                 "});"
                );

            ExtAspNet.PageContext.RegisterStartupScript
                  (
                  "var d=Ext.getCmp('" + ddlschool.ClientID + "');" +
                  " var oldInput=\"\";" +
                  " d.listWidth=500; " +
                  " d.addListener(\"specialkey\",function(f,e){ " +  //keyup
                  " var newInput=document.getElementById(\"" + ddlschool.ClientID + "\").value;" +
                   " if (oldInput==newInput) {return;} " +
                  " oldInput=newInput;" +
                   " __doPostBack('loadsc',newInput);" +
                   "});"
                  );

            ExtAspNet.PageContext.RegisterStartupScript
             (
             "var d=Ext.getCmp('" + ddlzj.ClientID + "');" +
             " var oldInput=\"\";" +
             " d.listWidth=500; " +
             " d.addListener(\"specialkey\",function(f,e){ " +  //keyup
             " var newInput=document.getElementById(\"" + ddlzj.ClientID + "\").value;" +
              " if (oldInput==newInput) {return;} " +
             " oldInput=newInput;" +
              " __doPostBack('loadzj',newInput);" +
              "});"
             );

            ExtAspNet.PageContext.RegisterStartupScript
              (
              "var d=Ext.getCmp('" + ddlsales.ClientID + "');" +
              " var oldInput=\"\";" +
              " d.listWidth=500; " +
              " d.addListener(\"specialkey\",function(f,e){ " +  //keyup
              " var newInput=document.getElementById(\"" + ddlsales.ClientID + "\").value;" +
               " if (oldInput==newInput) {return;} " +
              " oldInput=newInput;" +
               " __doPostBack('loadss',newInput);" +
               "});"
              );

        
            ExtAspNet.PageContext.RegisterStartupScript
            (
            "var d=Ext.getCmp('" + ddlowen.ClientID + "');" +
            " var oldInput=\"\";" +
            " d.listWidth=500; " +
            " d.addListener(\"specialkey\",function(f,e){ " +  //keyup
            " var newInput=document.getElementById(\"" + ddlowen.ClientID + "\").value;" +
             " if (oldInput==newInput) {return;} " +
            " oldInput=newInput;" +
             " __doPostBack('loadowen',newInput);" +
             "});"
            );
  

        }

        void fillcomp()
        {



            if (Request["__EVENTTARGET"] == "loadcp")
            {
                string sqltxt = "SELECT * FROM company where charindex('" +
                    Request["__EVENTARGUMENT"].ToString() + "',cname)>0 ";
                // txtnm.Text = Request["__EVENTARGUMENT"].ToString();

                DataTable dt = DAL.DbHelperSQL.SqlDataAdapter(sqltxt);
                ddlcomp.Items.Clear();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ddlcomp.Items.Add(new ExtAspNet.ListItem(dt.Rows[i]["cname"].ToString(), dt.Rows[i]["cname"].ToString()));
                }


                //下面这句话是有道理的，extjs默认效果在autocomplete之后，值为变成下拉框的第一项，有点破坏效果，这里纠正过来，可以输入多个关键字  
                ExtAspNet.PageContext.RegisterStartupScript("document.getElementById(\"" + ddlcomp.ClientID + "\").value='" +
                         Request["__EVENTARGUMENT"] + "';");

            }

            if (Request["__EVENTTARGET"] == "loadsc")
            {
                string sqltxt = "SELECT * FROM school where charindex('" +
                    Request["__EVENTARGUMENT"].ToString() + "',schname)>0 ";
                // txtnm.Text = Request["__EVENTARGUMENT"].ToString();

                DataTable dt = DAL.DbHelperSQL.SqlDataAdapter(sqltxt);
                ddlschool.Items.Clear();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ddlschool.Items.Add(new ExtAspNet.ListItem(dt.Rows[i]["schname"].ToString(), dt.Rows[i]["schbh"].ToString()));
                }


                //下面这句话是有道理的，extjs默认效果在autocomplete之后，值为变成下拉框的第一项，有点破坏效果，这里纠正过来，可以输入多个关键字  
                ExtAspNet.PageContext.RegisterStartupScript("document.getElementById(\"" + ddlschool.ClientID + "\").value='" +
                         Request["__EVENTARGUMENT"] + "';");

            }

            if (Request["__EVENTTARGET"] == "loadzj")
            {
                string sqltxt = "SELECT * FROM zj where charindex('" +
                    Request["__EVENTARGUMENT"].ToString() + "',zname)>0 ";
                // txtnm.Text = Request["__EVENTARGUMENT"].ToString();

                DataTable dt = DAL.DbHelperSQL.SqlDataAdapter(sqltxt);
                ddlzj.Items.Clear();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ddlzj.Items.Add(new ExtAspNet.ListItem(dt.Rows[i]["zname"].ToString(), dt.Rows[i]["zbh"].ToString()));
                }


                //下面这句话是有道理的，extjs默认效果在autocomplete之后，值为变成下拉框的第一项，有点破坏效果，这里纠正过来，可以输入多个关键字  
                ExtAspNet.PageContext.RegisterStartupScript("document.getElementById(\"" + ddlzj.ClientID + "\").value='" +
                         Request["__EVENTARGUMENT"] + "';");

            }


            if (Request["__EVENTTARGET"] == "loadss")
            {
                string sqltxt = "SELECT * FROM sys_user where charindex('" +
                    Request["__EVENTARGUMENT"].ToString() + "',user_name)>0 ";
                // txtnm.Text = Request["__EVENTARGUMENT"].ToString();

                DataTable dt = DAL.DbHelperSQL.SqlDataAdapter(sqltxt);
                ddlsales.Items.Clear();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ddlsales.Items.Add(new ExtAspNet.ListItem(dt.Rows[i]["user_name"].ToString(), dt.Rows[i]["sbh"].ToString()));
                }


                //下面这句话是有道理的，extjs默认效果在autocomplete之后，值为变成下拉框的第一项，有点破坏效果，这里纠正过来，可以输入多个关键字  
                ExtAspNet.PageContext.RegisterStartupScript("document.getElementById(\"" + ddlsales.ClientID + "\").value='" +
                         Request["__EVENTARGUMENT"] + "';");

            }


            if (Request["__EVENTTARGET"] == "loadowen")
            {
                string sqltxt = "SELECT * FROM sys_user where charindex('" +
                    Request["__EVENTARGUMENT"].ToString() + "',user_name)>0 ";
                // txtnm.Text = Request["__EVENTARGUMENT"].ToString();

                DataTable dt = DAL.DbHelperSQL.SqlDataAdapter(sqltxt);
                ddlowen.Items.Clear();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ddlowen.Items.Add(new ExtAspNet.ListItem(dt.Rows[i]["user_name"].ToString(), dt.Rows[i]["sbh"].ToString()));
                }


                //下面这句话是有道理的，extjs默认效果在autocomplete之后，值为变成下拉框的第一项，有点破坏效果，这里纠正过来，可以输入多个关键字  
                ExtAspNet.PageContext.RegisterStartupScript("document.getElementById(\"" + ddlowen.ClientID + "\").value='" +
                         Request["__EVENTARGUMENT"] + "';");

            }

        }



        //填 充ddl
        void fill2()
        {

            //公司
            DataTable e = BLL.Bllbase.get_table("[company]");
            ddlcomp.Items.Clear();

            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddlcomp.Items.Add(new ExtAspNet.ListItem(e.Rows[i]["cname"].ToString().Trim(), e.Rows[i]["cbh"].ToString().Trim()));

            }

            ddlcomp.DataBind();

            ddlcomp.Items.Add(new ExtAspNet.ListItem("", ""));

            ddlcomp.SelectedIndex = ddlcomp.Items.IndexOf(ddlcomp.Items.FindByText(""));




            //员工
            e = BLL.Bllbase.get_table("[sys_user]");
            ddlsales.Items.Clear();
            ddlowen.Items.Clear();
            for (int i = 0; i < e.Rows.Count; i++)
            {

                ddlsales.Items.Add(new ExtAspNet.ListItem(e.Rows[i]["user_name"].ToString().Trim(), e.Rows[i]["sbh"].ToString().Trim()));
                ddlowen.Items.Add(new ExtAspNet.ListItem(e.Rows[i]["user_name"].ToString().Trim(), e.Rows[i]["sbh"].ToString().Trim()));

            }

            ddlsales.DataBind();
            ddlowen.DataBind();
            ddlsales.Items.Add(new ExtAspNet.ListItem("", ""));
            ddlowen.Items.Add(new ExtAspNet.ListItem("", ""));
            ddlsales.SelectedIndex = ddlsales.Items.IndexOf(ddlsales.Items.FindByText(""));
            ddlowen.SelectedIndex = ddlowen.Items.IndexOf(ddlowen.Items.FindByText(""));

            //校园
            e = BLL.Bllbase.get_table("[school]");
            ddlschool.Items.Clear();
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddlschool.Items.Add(new ExtAspNet.ListItem(e.Rows[i]["schname"].ToString().Trim(), e.Rows[i]["schbh"].ToString()));
            }
            ddlschool.DataBind();
            ddlschool.Items.Add(new ExtAspNet.ListItem("", ""));

            ddlschool.SelectedIndex = ddlschool.Items.IndexOf(ddlschool.Items.FindByText(""));

            //中介
            e = BLL.Bllbase.get_table("[zj]");
            ddlzj.Items.Clear();
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddlzj.Items.Add(new ExtAspNet.ListItem(e.Rows[i]["zname"].ToString().Trim(), e.Rows[i]["zbh"].ToString()));
            }
            ddlzj.DataBind();
            ddlzj.Items.Add(new ExtAspNet.ListItem("", ""));

            ddlzj.SelectedIndex = ddlzj.Items.IndexOf(ddlzj.Items.FindByText(""));
        }
      

        //填 充ddl
        void fill()
        {
            BLL.HR.hr_ot u = new BLL.HR.hr_ot();
            DataTable e;

            //填充部门
            e = u.getlist_dept(whe);
            ddldetp.Items.Clear();
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddldetp.Items.Add(new ExtAspNet.ListItem(e.Rows[i][2].ToString(), e.Rows[i][2].ToString()));
            }
            ddldetp.DataBind();

            //填充职位
            e = u.getlist_job(whe);
            ddljob.Items.Clear();
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddljob.Items.Add(new ExtAspNet.ListItem(e.Rows[i][2].ToString(), e.Rows[i][2].ToString()));
            }
            ddljob.DataBind();


            //填充角色
            e = u.getlist_role(whe);
            ddlrole.Items.Clear();
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddlrole.Items.Add(new ExtAspNet.ListItem(e.Rows[i][2].ToString(), e.Rows[i][2].ToString()));
            }
            ddlrole.DataBind();

            //填充学历
            e = u.getlist_edu(whe);
            ddledu.Items.Clear();
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddledu.Items.Add(new ExtAspNet.ListItem(e.Rows[i][2].ToString(), e.Rows[i][2].ToString()));
            }
            ddledu.DataBind();


        }

        void fill3()
        {
            string dts; DateTime lastday;
            string ftype,fbh,fname;
        //  BLL.PQ.pq_member S = new BLL.PQ.pq_member();
          DataTable d = BLL.Bllbase.get_table(tab, "sid=" + hidsid.Text);// S.GetList("sid=" + hidsid.Text).Tables[0];


          hidsbh.Text = d.Rows[0]["sbh"].ToString().Trim();
          txtnm.Text = d.Rows[0]["user_name"].ToString().Trim();                       
      
          ddlsex.SelectedIndex = ddlsex.Items.IndexOf(ddlsex.Items.FindByText(d.Rows[0]["sex"].ToString()));

          txtschool.Text = d.Rows[0]["school"].ToString().Trim();

          ddledu.SelectedIndex = ddledu.Items.IndexOf(ddledu.Items.FindByText(d.Rows[0]["edu"].ToString()));

          major.Text = d.Rows[0]["major"].ToString().Trim();

          ddldetp.SelectedIndex = ddldetp.Items.IndexOf(ddldetp.Items.FindByText(d.Rows[0]["dept"].ToString()));

          ddlrole.SelectedIndex = ddlrole.Items.IndexOf(ddlrole.Items.FindByText(d.Rows[0]["role"].ToString()));

          ddljob.SelectedIndex = ddljob.Items.IndexOf(ddljob.Items.FindByText(d.Rows[0]["job"].ToString()));
          ddlmarry.SelectedIndex = ddlmarry.Items.IndexOf(ddlmarry.Items.FindByText(d.Rows[0]["marry"].ToString()));


          txtjg.Text = d.Rows[0]["jg"].ToString().Trim();
          txtaddress.Text = d.Rows[0]["hj_address"].ToString().Trim();
          txtnewadd.Text = d.Rows[0]["cur_address"].ToString().Trim();
          txtcode.Text = d.Rows[0]["code"].ToString().Trim();
          txttel.Text = d.Rows[0]["tel"].ToString().Trim();
          txtjustperson.Text = d.Rows[0]["r_name"].ToString().Trim();
          txtjusttel.Text = d.Rows[0]["r_tel"].ToString().Trim();
          txtqq.Text = d.Rows[0]["qq"].ToString().Trim();
          txtemail.Text = d.Rows[0]["email"].ToString().Trim();
            ftype=d.Rows[0]["ftype"].ToString().Trim();
            fbh=d.Rows[0]["fbh"].ToString().Trim();
            fname=d.Rows[0]["fname"].ToString().Trim();



            string rdate = d.Rows[0]["birthday"].ToString().Trim();
            if (rdate.Substring(0, 4) != "1900")
            {
                 lastday = DateTime.Parse(rdate);
                 birthday.Text = lastday.ToString("yyyy-M-d");
            }

          dts = d.Rows[0]["indate"].ToString().Trim();
          if (dts.Substring(0, 4) != "1900")
          {
               lastday = DateTime.Parse(dts);
              indate.Text = lastday.ToString("yyyy-M-d");
          }

          dts = d.Rows[0]["outdate"].ToString().Trim();
          if (dts.Substring(0, 4) != "1900")
          {
               lastday = DateTime.Parse(dts);
              outdate.Text = lastday.ToString("yyyy-M-d");
          }
         
          txtlgid.Text = d.Rows[0]["lgid"].ToString().Trim();
          
          //txtlgpw.Text = d.Rows[0]["lgpw"].ToString().Trim();
          hidpassword.Text = d.Rows[0]["lgpw"].ToString().Trim();

          ddlstatus.SelectedIndex = ddlstatus.Items.IndexOf(ddlstatus.Items.FindByText(d.Rows[0]["status"].ToString()));

          ddlinout.SelectedIndex = ddlinout.Items.IndexOf(ddlinout.Items.FindByText(d.Rows[0]["inout"].ToString()));
          
          Image1.ImageUrl = d.Rows[0]["pic"].ToString();

          if (ftype == "企业")
          {
              ddlcomp.SelectedIndex = ddlcomp.Items.IndexOf(ddlcomp.Items.FindByText(d.Rows[0]["fname"].ToString()));
          }
          if (ftype == "学校")
          {
              ddlschool.SelectedIndex = ddlschool.Items.IndexOf(ddlschool.Items.FindByText(d.Rows[0]["fname"].ToString()));
          }
          if (ftype == "中介")
          {
              ddlzj.SelectedIndex = ddlzj.Items.IndexOf(ddlzj.Items.FindByText(d.Rows[0]["fname"].ToString()));
          }

          if (ftype == "业务员")
          {
              ddlsales.SelectedIndex = ddlsales.Items.IndexOf(ddlsales.Items.FindByText(d.Rows[0]["fname"].ToString()));
          }

            //责任人
          ddlowen.SelectedIndex = ddlowen.Items.IndexOf(ddlowen.Items.FindByText(d.Rows[0]["owen"].ToString()));
          

        }
  
        protected void btnSubmit_Click(object sender, EventArgs e)
        {  
            //文件上传
          
            string fileName = fileup.ShortFileName;
             if (fileName.Length >0)
                    {
                        fileName = fileName.Replace(":", "_").Replace(" ", "_").Replace("\\", "_").Replace("/", "_");
                        fileName = DateTime.Now.Ticks.ToString() + "_" + fileName;
                        path = "~/file/hr/" + fileName;
                        if (fileup.HasFile)
                            fileup.SaveAs(Server.MapPath(path));
                    }
             up_member();


           //string title = txttitel.Text;
            //string person =us;
           // string day = txtday.Text;
          //  string memo = HttpUtility.HtmlEncode(FCKeditor1.Value);
          //  string memo = HttpUtility.HtmlEncode(FCKeditor1.Value);
        
         

           // BLL.oa.ad.insert(title, person, day, memo, path, hidstype.Text);


            Alert.Show("添加完成 ");
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        void up_member()
        {
            Model.PQ.pq_member s = new Model.PQ.pq_member();
            s.sid =int.Parse(hidsid.Text);
            s.sbh = hidsbh.Text;

            if (path == "")
                path = Image1.ImageUrl;

            s.lgid = txtlgid.Text;

            if (txtlgpw.Text != "")
                s.lgpw = BLL.sys.chkpw.Md5(txtlgpw.Text);
            else
                s.lgpw = hidpassword.Text;


            s.user_name = txtnm.Text;
            s.sex = ddlsex.SelectedValue;

            if (VAN.IsDateTime(indate.Text))
                s.indate = DateTime.Parse(indate.Text);
            else
                s.indate = DateTime.Parse("1900-01-01");

            if (VAN.IsDateTime(birthday.Text))
                s.birthday = DateTime.Parse(birthday.Text);
            else
                s.birthday = DateTime.Parse("1900-01-01");

            if (VAN.IsDateTime(outdate.Text))
                s.outdate = DateTime.Parse(outdate.Text);
            else
                s.outdate =DateTime.Parse( "1900-01-01");

            s.school = "";
            s.school=txtschool.Text;

            s.edu = "";
            s.edu  =ddledu.SelectedValue;

            s.major = "";
            s.major = major.Text;

            s.dept = "";
          //  s.dept = ddldetp.SelectedValue;

            s.role = "";
          //  s.role = ddlrole.SelectedValue;

            s.job = "";
            //s.job = ddljob.SelectedValue;

            s.jg = "";
            s.jg = txtjg.Text;

            s.hj_address = "";
            s.hj_address = txtaddress.Text;

            s.cur_address = "";
            s.cur_address = txtnewadd.Text;

            s.code = "";
            s.code = txtcode.Text;

            s.tel = "";
            s.tel = txttel.Text;

            s.r_name = "";
            s.r_name = txtjustperson.Text;

            s.r_tel = "";
            s.r_tel = txtjusttel.Text;

            s.QQ = "";
            s.QQ = txtqq.Text;

            s.email = "";
            s.email = txtemail.Text;

            s.marry = "";
            s.marry = ddlmarry.SelectedValue; //婚否
            s.pic = path;
            s.status = 0; //开通
            s.status =int.Parse( ddlstatus.SelectedValue);
            s.inout =int.Parse(ddlinout.SelectedValue);  //在职
            s.ftype = "";
            s.fbh = "";
            s.fname = "";
            if (ddlcomp.SelectedValue != "")
            {
                s.ftype = "企业";
                s.fbh = ddlcomp.SelectedValue;
                s.fname = ddlcomp.SelectedText;
            }

            if (ddlschool.SelectedValue != "")
            {
                s.ftype = "学校";
                s.fbh = ddlschool.SelectedValue;
                s.fname = ddlschool.SelectedText;
            }


            if (ddlzj.SelectedValue != "")
            {
                s.ftype = "中介";
                s.fbh = ddlzj.SelectedValue;
                s.fname = ddlzj.SelectedText;
            }

            if (ddlsales.SelectedValue != "")
            {
                s.ftype = "业务员";
                s.fbh = ddlsales.SelectedValue;
                s.fname = ddlsales.SelectedText;
            }

            BLL.PQ.pq_member  t = new BLL.PQ.pq_member();
            t.Update(s);

            string sqltxt = "update  pq_member set  owendtime=getdate(),updtime=getdate(), sales='" + um + "',owen='" +
                              ddlowen.SelectedText + "' where 1=1 and sbh='" + s.sbh + "' ";


            DAL.DbHelperSQL.ExecuteNonQuery(sqltxt);
        }
       
    }
}